package com.woniu.account.controller;


import com.woniu.account.dto.*;
import com.woniu.account.form.*;
import com.woniu.account.model.TCoach;
import com.woniu.account.service.TCoachService;
import com.woniu.core.util.JwtUtil;

import com.woniu.core.util.PageInfo;
import com.woniu.core.util.RedisUtil;
import com.woniu.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@RestController
@RequestMapping("/tCoach")
@Api(tags = "教练表相关接口")
public class TCoachController {
    @Resource
    private TCoachService tCoachService;
    @Resource
    private RedisUtil redisUtil;
    @ApiOperation("根据id查询教练账号信息")
    @GetMapping("/getCoachAccountInfoById/{coachId}.coach")
    public Result<CoachDto> getCoachAccountById(@PathVariable("coachId") Integer coachId){
        CoachDto dto = tCoachService.getCoachById(coachId);
        return Result.getResult(CoachDto.class).setMsg("查询成功").setData(dto);
    }

//    @Autowired
//    private TCoachService tCoachService;

    @ApiOperation("教练账号注册")
    @PostMapping("/teacherRegister.coach")
    public Result teacherRegister(@RequestBody @Valid RegisterCoachForm form) throws Exception{
        TCoach tCoach= tCoachService.teacherRegister(form);
        return Result.getResult(TCoach.class).setMsg("注册成功").setData(tCoach);
    }

    @PostMapping("/sendRegisterCode")
    @ApiOperation("教练发送注册短信验证码")
    public Result sendRegisterCode(String phone) throws Exception{
        tCoachService.sendRegisterCode(phone);
        return Result.getResult(Boolean.class).setMsg("发送验证码成功").setData(true);
    }

    @ApiOperation("教练账号密码登录")
    @PostMapping("/teacherLogin.coach")
    public Result teacherLogin(@RequestBody @Valid LoginCoachForm form) throws Exception{
        CoachLoginDto userCoachLoginDTO = tCoachService.teacherLogin(form);
        return Result.getResult(CoachLoginDto.class).setMsg("登录成功").setData(userCoachLoginDTO);
    }

    //发送登录验证码：
    @PostMapping("/sendLoginCode/{phone}")
    @ApiOperation("教练发送登录验证码")
    //根据你的token里面带的角色动态生成菜单
    public Result sendLoginCode(@PathVariable String phone) throws Exception{
        System.out.println(phone);
        tCoachService.sendLoginCode(phone);
        return Result.getResult(Boolean.class).setMsg("发送成功").setData(true);
    }

    @ApiOperation("教练手机验证码登录")
    @PostMapping("/coachCodeLogin.coach")
    public Result coachCodeLogin(@RequestBody @Valid CoachCodeLoginForm form) throws Exception{
        CoachLoginDto userCoachLoginDTO = tCoachService.coachCodeLogin(form);
        return Result.getResult(CoachLoginDto.class).setMsg("登录成功").setData(userCoachLoginDTO);
    }

    @ApiOperation("教练修改密码")
    @PostMapping("/coachChangePassword.coach")
    public Result coachChangePassword(@RequestBody @Valid ChangePasswordForm form) throws Exception{
        tCoachService.coachChangePassword(form);
        return Result.getResult(Boolean.class).setMsg("修改成功").setData(true);
    }



    @ApiOperation("教练账号信息完善")
    @PostMapping("/coachAccountPerfect.coach")
    public Result coachAccountPerfect(@RequestBody @Valid updateCoachInformationForm form)throws Exception{
        TCoach tCoach=tCoachService.coachAccountPerfect(form);
        return Result.getResult(TCoach.class).setMsg("修改成功").setData(tCoach);
    }

    @ApiOperation("根据coachId绑定签约的场馆")
    @PostMapping("/coachBindGym/{coachId}/{gymNum}.coach")
    public Result<CoachDto> coachBindGym(@PathVariable("coachId") Integer coachId,@PathVariable("gymNum") String gymNUm)throws Exception{
        CoachDto tCoach=tCoachService.coachBindGym(coachId,gymNUm);
        return Result.getResult(CoachDto.class).setMsg("绑定成功").setData(tCoach);
    }

    @PostMapping("/uploadImage.coach")
    @ApiOperation("教练更改图片")
    //如果参数是MultipartFile来传参，就要分开；如果用String在参数对象就可以用参数对象而且要用@requestBody注解
    public Result uploadImage(MultipartFile image) throws Exception {
        Integer coachId = JwtUtil.getUserId(3);
        UpdateImageForm form = new UpdateImageForm();
        form.setImage(image);
        form.setAccountId(coachId);
        String s = tCoachService.uploadImage(form);
        return Result.getResult(String.class).setMsg("更改成功").setData(s);
    }
    @ApiOperation("根据coachId解绑场馆")
    @PostMapping("/coachUntieGym/{coachId}.coach")
    public Result<CoachDto> coachUntieGym(@PathVariable("coachId") Integer coachId)throws Exception{
        CoachDto tCoach=tCoachService.coachUntieGym(coachId);
        return Result.getResult(CoachDto.class).setMsg("解绑成功").setData(tCoach);
    }
    @ApiOperation("教练查询自己的粉丝列表")
    @GetMapping("/myFans.coach")
    public Result myFans(Integer pageNum,Integer pageSize,@RequestHeader String token) throws Exception {
        String refreshToken = redisUtil.get(token);
        Integer coachId = Integer.valueOf(JwtUtil.parseToken(refreshToken,3).get("coachId").toString());
        PageInfo<MyFansDto> dto = tCoachService.myFans(pageNum,pageSize,coachId);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setData(dto);
    }
    @ApiOperation("查询所有未签约的教练且简历是公开的")
    @GetMapping("/findUnsignCoach")
    public Result findUnsignCoach(FindUnsignCoachForm form){
        PageInfo<FindUnsignedCoachDto> dto = tCoachService.findUnsignCoach(form);
        return Result.getResult(PageInfo.class).setMsg("查询成功").setData(dto);
    }

    //通过userId来查user的相关信息
    @ApiOperation("教练查询自己的学员或根据课程类别查询")
    @GetMapping("/queryMyStudentByCondition.coach")
    public Result queryMyStudentByCondition(Integer courseVariety) throws Exception{
        List<CoachqueryMyStudentDto> dto = tCoachService.queryMyStudentByCondition(courseVariety);
        return Result.getListResult(CoachqueryMyStudentDto.class).setMsg("查询成功").setData(dto);
    }

}
